package leetcode.code0655;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution implements IDebug {

	int h;

	public List<List<String>> printTree(TreeNode root) {
		int m = height(root);
		h = m - 1;
		int n = (int) Math.pow(2, m) - 1;
		List<String> list = new ArrayList<>();
		for (int i = 0; i < n; i++) {
			list.add("");
		}
		List<List<String>> ans = new ArrayList<>();
		for (int i = 0; i < m; i++) {
			ans.add(new ArrayList<>(list));
		}
		fill(root, 0, (n - 1) >> 1, ans);
		return ans;
	}

	private void fill(TreeNode root, int r, int c, List<List<String>> ans) {
		if (root == null) {
			return;
		}
		ans.get(r).set(c, String.valueOf(root.val));
		int index = (int) Math.pow(2, h - r - 1);
		this.fill(root.left, r + 1, c - index, ans);
		this.fill(root.right, r + 1, c + index, ans);
	}

	private int height(TreeNode root) {
		if (root == null) {
			return 0;
		}
		int ans = this.height(root.left);
		ans = Math.max(ans, this.height(root.right));
		return ans + 1;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
